//package com.hangxintong.common.utils.aop;
//
//import com.hangxintong.common.base.entity.LmUser;
//import com.hangxintong.common.base.entity.SystemGlobalLog;
//import com.hangxintong.common.base.service.SystemGlobalLogService;
//import com.hangxintong.common.base.service.SystemGlobalLogogService;
//import com.hangxintong.common.config.HeaderHolder;
//import com.hangxintong.common.exception.SystemErrorCodeMark;
//import com.hangxintong.common.exception.SystemException;
//import com.hangxintong.common.utils.Result;
//import com.hangxintong.common.utils.SnowflakeId;
//import com.hangxintong.common.utils.annon.Logoutput;
//import com.hangxintong.common.utils.annon.NoRepeatSubmit;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.util.ObjectUtils;
//import org.springframework.util.StringUtils;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.Calendar;
//import java.util.Map;
//
///**
// * @Auther: tsj
// * @Date: 2021/3/1 14:14
// * @Description: 执行方法日志输出
// */
//@Aspect
//@Component
//@Slf4j
//public class LogoutputAspect {
//
//
//    @Autowired
//    SystemGlobalLogService systemLogService;
//
//    @Autowired
//    HeaderHolder headerHolder;
//
//    @Autowired
//    SnowflakeId snowflakeId;
//
//    /**
//     * 设置注解切点
//     *
//     * @param logoutput
//     */
//    @Pointcut("@annotation(logoutput)")
//    public void repeatPoint(Logoutput logoutput) {
//
//    }
//
//    /**
//     * 设置注解环绕增强
//     */
//    @AfterReturning(value = "repeatPoint(logoutput)")
//    public void doAfterReturning(JoinPoint joinPoint, Logoutput logoutput) {
//
//        try {
//
//             SystemGlobalLog systemLog = getSystemLog(joinPoint,logoutput, 1);
//
//            try {
////                Object proceed = joinPoint.proceed();;
////                return proceed;
//            } catch (Throwable throwable) {
//                systemLog.setErrorMessage(throwable.getMessage());
//                systemLog.setStatus(0);
//                log.error("运行业务代码出错", throwable);
//                throw new RuntimeException(throwable.getMessage());
//            } finally {
//
//                systemLogService.saveSystemLog(systemLog);
//            }
//
//        } catch (Exception e) {
//            throw new SystemException(SystemErrorCodeMark.Code.SYSTEM_ERROR_LOCAL, e.getMessage());
//        }
//    }
//
//
//
//    /**
//     * 设置异常增强
//     */
//    @AfterThrowing(value = "repeatPoint(logoutput)",throwing = "ex")
//    public void doAfterThrowing(JoinPoint  joinPoint, Logoutput logoutput,Throwable ex) {
//
//        try {
//
//             SystemGlobalLog systemLog = this.getSystemLog(joinPoint, logoutput, 0,ex.getMessage());
//
//            try {
////                Object proceed = joinPoint.proceed();
////                return proceed;
//            } catch (Throwable throwable) {
//                systemLog.setErrorMessage(throwable.getMessage());
//                systemLog.setStatus(0);
//                log.error("运行业务代码出错", throwable);
//                throw new RuntimeException(throwable.getMessage());
//            } finally {
//
//                systemLogService.saveSystemLog(systemLog);
//            }
//
//        } catch (Exception e) {
//            throw new SystemException(SystemErrorCodeMark.Code.SYSTEM_ERROR_LOCAL, e.getMessage());
//        }
//    }
//
//
//    public SystemGlobalLog getSystemLog(JoinPoint  joinPoint,Logoutput logoutput,Integer status){
//        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = servletRequestAttributes.getRequest();
//
//        String cid = null;
//        //获取企业id
//        cid = request.getParameter("cid");
//        if (StringUtils.isEmpty(cid)) {
//            //如果没有通过参数找到可以去heder中查询
//             cid = request.getHeader("cid");
//        }
//        Object[] args = joinPoint.getArgs();
//        String method = logoutput.method();
//        String methodName = logoutput.methodName();
//
//        LmUser user = headerHolder.getHeader().getUser();
//        String xid = headerHolder.getHeader().getXID();
//        if (ObjectUtils.isEmpty(user)) {
//            throw new SystemException(SystemErrorCodeMark.Code.USER_NO_PERMISSION, "未获取到登录用户信息!");
//        }
//
//        SystemGlobalLog systemGlobalLog = new SystemGlobalLog(snowflakeId.nextIdStr(), user.getId(), user.getUsername(), method, Calendar.getInstance().getTime(), methodName, status, xid, org.apache.commons.lang3.StringUtils.isBlank(cid)?0:Integer.valueOf(cid),null);
//        return  systemGlobalLog;
//    }
//
//    public SystemGlobalLog getSystemLog(JoinPoint  joinPoint,Logoutput logoutput,Integer status,String errorMessage) {
//         SystemGlobalLog systemLog = getSystemLog(joinPoint, logoutput, status);
//         systemLog.setErrorMessage(errorMessage);
//         return systemLog;
//    }
//    }
